package de.midnightdance.client.model;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Iterator;
import java.util.List;

import de.midnightdance.client.model.interfaces.IModelClient;
import de.midnightdance.client.ws.model.entities.CharacterRequiemID;
import de.midnightdance.client.ws.model.entities.CharacterRequiemMensch;

public class ModelClient implements IModelClient {
	public static final String 	PROPERTY_LIST_CHARACTERS 	= "listCharacters";
	public static final String 	PROPERTY_ACTIVE_CHARACTER	= "activeCharacter";	
	//
	public static final int 	INDEX_LIST_CHARACTERS 		= 2;
	public static final int 	INDEX_ACTIVE_CHARACTER		= 3;
	
	private List<CharacterRequiemID> 	listCharacters;
	private CharacterRequiemMensch		characterActive;
	
	private PropertyChangeSupport propertyChangeSupport;	
	
	public ModelClient() {
		super();
		propertyChangeSupport 	= new PropertyChangeSupport(this);
		activeCharacterPCL 		= new ActiveCharacterPCL();
	}
	
	
	/* (non-Javadoc)
	 * @see de.midnightdance.client.model.IModelClient#registerPropertyChangeListener(java.beans.PropertyChangeListener)
	 */
	public void registerPropertyChangeListener(PropertyChangeListener listener){
		propertyChangeSupport.addPropertyChangeListener(listener);
	}
	/* (non-Javadoc)
	 * @see de.midnightdance.client.model.IModelClient#deRegisterPropertyChangeListener(java.beans.PropertyChangeListener)
	 */
	public void deRegisterPropertyChangeListener(PropertyChangeListener listener){
		propertyChangeSupport.removePropertyChangeListener(listener);
	}	
	
	
	/* (non-Javadoc)
	 * @see de.midnightdance.client.model.IModelClient#getCharacters()
	 */
	public Iterator<CharacterRequiemID> getCharacters(){
		return listCharacters.iterator();
	}
	/* (non-Javadoc)
	 * @see de.midnightdance.client.model.IModelClient#addCharacter(de.midnightdance.model.character.CharacterRequiemID)
	 */
	public boolean addCharacter(CharacterRequiemID characterId){		
		boolean success = listCharacters.add(characterId);
		propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_LIST_CHARACTERS,INDEX_LIST_CHARACTERS, null,listCharacters);
		return success;
	}
	/* (non-Javadoc)
	 * @see de.midnightdance.client.model.IModelClient#removeCharacter(de.midnightdance.model.character.CharacterRequiemID)
	 */
	public boolean removeCharacter(CharacterRequiemID characterId){
		boolean success = listCharacters.remove(characterId);
		propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_LIST_CHARACTERS,INDEX_LIST_CHARACTERS, null,listCharacters);
		return success;
	}	
	/* (non-Javadoc)
	 * @see de.midnightdance.client.model.IModelClient#setCharacters(java.util.List)
	 */
	public void setCharacters(List<CharacterRequiemID> characters){
		this.listCharacters = characters;
		propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_LIST_CHARACTERS,INDEX_LIST_CHARACTERS, null,listCharacters);
	}
	
	/* (non-Javadoc)
	 * @see de.midnightdance.client.model.IModelClient#getActiveCharacter()
	 */
	public CharacterRequiemMensch getActiveCharacter(){
		return characterActive;
	}		
	
	private void registerActiveCharacterListeners(){
		if (characterActive != null) {
			characterActive.registerPropertyChangeListener(activeCharacterPCL);
		}		
	}
	
	private void deRegisterActiveCharacterListeners(){
		if (characterActive != null) {
			characterActive.deRegisterPropertyChangeListener(activeCharacterPCL);
		}		
	}
	
	/* (non-Javadoc)
	 * @see de.midnightdance.client.model.IModelClient#setActiveCharacters(de.midnightdance.model.character.CharacterRequiem)
	 */
	public void setActiveCharacters(CharacterRequiemMensch characterRequiem){
		deRegisterActiveCharacterListeners();
		this.characterActive= characterRequiem;
		registerActiveCharacterListeners();		
	}
	
	private ActiveCharacterPCL activeCharacterPCL;
	private class ActiveCharacterPCL implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			//einfaches weiterreichen eines PCE aus dem CharacterRequiem
			propertyChangeSupport.firePropertyChange(evt);			
		}		
	}
}
